package org.dromara.wmscloud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.wmscloud.domain.bo.BaseMatGroupBo;
import org.dromara.wmscloud.domain.vo.BaseMatGroupVo;
import org.dromara.wmscloud.domain.BaseMatGroup;
import org.dromara.wmscloud.mapper.BaseMatGroupMapper;
import org.dromara.wmscloud.service.IBaseMatGroupService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 物料组Service业务层处理
 *
 * @author hou
 * @date 2025-04-04
 */
@RequiredArgsConstructor
@Service
public class BaseMatGroupServiceImpl implements IBaseMatGroupService {

    private final BaseMatGroupMapper baseMatGroupMapper;

    /**
     * 查询物料组
     *
     * @param groupId 主键
     * @return 物料组
     */
    @Override
    public BaseMatGroupVo queryById(Long groupId){
        return baseMatGroupMapper.selectVoById(groupId);
    }

    /**
     * 分页查询物料组列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 物料组分页列表
     */
    @Override
    public TableDataInfo<BaseMatGroupVo> queryPageList(BaseMatGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseMatGroup> lqw = buildQueryWrapper(bo);
        Page<BaseMatGroupVo> result = baseMatGroupMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的物料组列表
     *
     * @param bo 查询条件
     * @return 物料组列表
     */
    @Override
    public List<BaseMatGroupVo> queryList(BaseMatGroupBo bo) {
        LambdaQueryWrapper<BaseMatGroup> lqw = buildQueryWrapper(bo);
        return baseMatGroupMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseMatGroup> buildQueryWrapper(BaseMatGroupBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseMatGroup> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BaseMatGroup::getGroupId);
        lqw.eq(StringUtils.isNotBlank(bo.getGroupCode()), BaseMatGroup::getGroupCode, bo.getGroupCode());
        lqw.like(StringUtils.isNotBlank(bo.getGroupName()), BaseMatGroup::getGroupName, bo.getGroupName());
        return lqw;
    }

    /**
     * 新增物料组
     *
     * @param bo 物料组
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BaseMatGroupBo bo) {
        BaseMatGroup add = MapstructUtils.convert(bo, BaseMatGroup.class);
        validEntityBeforeSave(add);
        boolean flag = baseMatGroupMapper.insert(add) > 0;
        if (flag) {
            bo.setGroupId(add.getGroupId());
        }
        return flag;
    }

    /**
     * 修改物料组
     *
     * @param bo 物料组
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BaseMatGroupBo bo) {
        BaseMatGroup update = MapstructUtils.convert(bo, BaseMatGroup.class);
        validEntityBeforeSave(update);
        return baseMatGroupMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseMatGroup entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除物料组信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMatGroupMapper.deleteByIds(ids) > 0;
    }
}
